<?php

namespace app\admin\service;

use app\common\helper\MyHelper;

class ArticleService
{


    function getArticleDaiContentList($value, $content)
    {


        $target_area = json_decode($value['target_area'], true);
        $target_area_multiple = json_decode($value['target_area_multiple'], true);
        $articleService = new  ArticleService();
        $data['targetAreaType'] = $target_area['targetAreaType'];
        $data['targetAreaTypeRegular'] = $target_area['targetAreaTypeRegular'];;
        $data['regionalMultipleType'] = $target_area_multiple['regionalMultipleType'];
        $data['regionalMultipleTypeRegular'] = $target_area_multiple['regionalMultipleTypeRegular'];


        $targetAreaType = $data['targetAreaType'];
        $targetAreaTypeRegular = $data['targetAreaTypeRegular'];
        if ($targetAreaType == 3 && empty($targetAreaTypeRegular)) {
            return array('code' => 400, "data" => array(), 'message' => '请输入正则表达式');
        }


        $content = $content;

        //目标区域 正则
        if ($targetAreaType == 3) {
            $result = targetArea($content, $targetAreaTypeRegular);
            $content = $result['data'];
        }

        //目标区域多项
        $regionalMultipleType = $data['regionalMultipleType'];
        $list = array();


        if ($regionalMultipleType == 1) {
            $list[] = $content;
        } else {

            if (empty($data['regionalMultipleTypeRegular'])) {
                return array('code' => 400, "data" => array(), 'message' => '填写目标区域内是否多项内容正则');
            }

            $pattern = $data['regionalMultipleTypeRegular'];

            $result = multipleTargetArea($content, $pattern);
            if ($result ['code'] == 400) {
                return $result;
            }
            $list = $result['data'];
        }

        return array('code' => 200, "data" => $list, 'message' => '获取成功');

    }


    function getArticleList($data)
    {


        $id = $data['id'];
        $targetAreaType = $data['targetAreaType'];
        $targetAreaTypeRegular = $data['targetAreaTypeRegular'];
        if ($targetAreaType == 3 && empty($targetAreaTypeRegular)) {
            return array('code' => 400, "data" => array(), 'message' => '请输入正则表达式');
        }

        $result = MyHelper::getArticleInfoById($id);
        $content = $result['data']['content'];

        //目标区域 正则
        if ($targetAreaType == 3) {
            $result = targetArea($content, $targetAreaTypeRegular);
            $content = $result['data'];
        }

        //目标区域多项
        $regionalMultipleType = $data['regionalMultipleType'];
        $list = array();


        if ($regionalMultipleType == 1) {
            $list[] = $content;
        } else {

            if (empty($data['regionalMultipleTypeRegular'])) {
                return array('code' => 400, "data" => array(), 'message' => '填写目标区域内是否多项内容正则');
            }

            $pattern = $data['regionalMultipleTypeRegular'];

            $result = multipleTargetArea($content, $pattern);
            if ($result ['code'] == 400) {
                return $result;
            }
            $list = $result['data'];
        }

        return array('code' => 200, "data" => $list, 'message' => '获取成功');

    }

    /** 实体类型
     * @param $data
     * @return array|void
     */
    function entityTypeAlidate($data)
    {
        if (empty($data['entityType'])) {
            return array('code' => 400, "data" => array(), 'message' => '填写实体类型');
        }

        if (empty($data['entityRegOrDic'])) {
            return array('code' => 400, "data" => array(), 'message' => '请选择正则或者字典');
        }
        if ($data['entityRegOrDic'] == 1) {
            if (empty($data['entityReg'])) {
                return array('code' => 400, "data" => array(), 'message' => '填写正则');
            }
        }
        if ($data['entityRegOrDic'] == 2) {
            if (empty($data['entityDic'])) {
                return array('code' => 400, "data" => array(), 'message' => '请选择字典');
            }
        }
        if (empty($data['entityRadio'])) {
            return array('code' => 400, "data" => array(), 'message' => '请选择单个目标或者多个目标');
        }

        return array('code' => 200, "data" => array(), 'message' => '验证通过');
    }

    /** 验证参数
     * @param $data
     * @return array
     */
    function attributeNameAlidate($data)
    {

        if (empty($data['attributeName'])) {
            return array('code' => 400, "data" => array(), 'message' => '填写属性名称');
        }

        if (empty($data['attributeRegOrDic'])) {
            return array('code' => 400, "data" => array(), 'message' => '请选择属性值正则或者字典');
        }
        if ($data['attributeRegOrDic'] == 1) {
            if (empty($data['attributeReg'])) {
                return array('code' => 400, "data" => array(), 'message' => '填写属性值正则');
            }
        }
        if ($data['attributeRegOrDic'] == 2) {
            if (empty($data['attributeDic'])) {
                return array('code' => 400, "data" => array(), 'message' => '请选择属性值字典');
            }
        }
        if (empty($data['attributeRadio'])) {
            return array('code' => 400, "data" => array(), 'message' => '请选择属性值单个目标或者多个目标');
        }

        return array('code' => 200, "data" => array(), 'message' => '验证通过');


    }

    function inductiveAttributeListData($extractContent, $list)
    {



        $attributelist = array();
        foreach ($extractContent as $key => $value) {

            //属性验证
            $articleService = new ArticleService();
            $resultAttributeAlidate = $articleService->attributeNameAlidate($value);


            if ($resultAttributeAlidate ['code'] != 200) {
                return $resultAttributeAlidate;
            }
            $regOrDic = $value['attributeRegOrDic'];
            $radio = $value['attributeRadio'];
            $reg = $value['attributeReg'];
            $dic = $value['attributeDic'];

            $resultAttribute = MyHelper:: getHeadEntity($regOrDic, $list, $radio, $reg, $dic);
            if ($resultAttribute['code'] != 200) {
                continue;
            }

            $attributeName = $value['attributeName'];

            $attributelist[$key] = array("attributeName" => $attributeName, "attributeList" => $resultAttribute['data']);
        }

        return array('code' => 200, "data" => $attributelist, 'message' => '获取成功');

    }

    /** 获取头实体
     * @return \think\response\Json
     */
    function getHead($data, $list)
    {


        $type = $data['type'];

        if ($type == 1) {
            $entityText = "头实体";
        } else {
            $entityText = "尾实体";
        }


        $headRegOrDic = $data['headRegOrDic'];

        $regOrDic = "";
        if ($headRegOrDic == 1) {
            $regOrDic = "正则";
        } else {
            $regOrDic = "字典";
        }


        $headReg = $data['headReg'];
        $headRadio = $data['headRadio'];
        $headDic = $data['headDic'];

        $headRegAndDic = "";
        if (!empty($data['headRegAndDic'])) {
            $headRegAndDic = $data['headRegAndDic'];
        }

        $guding = "";
        if (!empty($data['guding'])) {
            $guding = $data['guding'];
        }


        $result = MyHelper::getHeadEntity($headRegOrDic, $list, $headRadio, $headReg, $headDic, $headRegAndDic, $guding);


        if ($result['code'] == 200) {
            $forlist = $result['data'];
            return array('code' => 200, "data" => $forlist, 'message' => '获取成功');
        } else {

            return $result;

        }

    }


}